package com.qc.controller.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qc.entity.UserEntityVo;
import com.qc.pageUtil.page.page.Page;
import com.qc.pageUtil.page.page.PageModel;
import com.qc.pageUtil.page.page.PageModelList;
import com.qc.pageUtil.page.vo.NewsAndNav;
import com.qc.service.UserService;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Controller
@RequestMapping("/web")
public class WebSecController{


    @Resource
    private UserService userService;
    
    /**
     * 跳转到首页
     * @return
     */
    @RequestMapping(value = "/toSecList", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public ModelAndView toSecList(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/sec/sec_list");
        
        String userRole = request.getParameter("userRole");
        
        //查询待审核总数
        int coutAuditing = userService.coutAuditing(userRole);
        mav.addObject("coutAuditing",coutAuditing);
        return mav;
    }
    
    @RequestMapping(value = "/toListAllUser", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllUser(HttpServletRequest request, HttpServletResponse response) {
        
        String userName = request.getParameter("userName");
        String isAuth = request.getParameter("isAuth");
        String sex = request.getParameter("sex");
        String userRole = request.getParameter("userRole");
        
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = userService.getListUserCount(userName, isAuth, sex,userRole);
        
        //根据条件查询当前页数据
        List<UserEntityVo> list = userService.getAllUser(userName, isAuth, sex,"", pageNo, pageSize);
        
        //分页查询公共类
        Page<UserEntityVo> page = new Page<UserEntityVo>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<UserEntityVo> pageModel = new PageModelList<UserEntityVo>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
    }
    
    /**
     * 秘书认证信息
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toSecInformAdd")
    public ModelAndView toSecInformAdd(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/sec/sec_add");
        
        return mav;
    }
    
    /**
     * 查看用户详细信息
     * @return
     */
    @RequestMapping(value = "/toSecInform", produces = "text/html;charset=UTF-8")
    public ModelAndView toSecInform(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/sec/sec_inform");
        String userId = request.getParameter("userId");
        
        UserEntityVo userEntityVo = userService.getUserId(userId);
        
        mav.addObject("userEntityVo",userEntityVo);
        
        return mav;
    }
    
    /**
     * 秘书认证信息
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toSecInform2")
    public ModelAndView toSecInform2(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/sec/sec_inform2");
        String userId = request.getParameter("userId");
        
        UserEntityVo userEntityVo = userService.getUserId(userId);
        
        mav.addObject("userEntityVo",userEntityVo);
        
        return mav;
    }
    
    @RequestMapping(value = "/toDeleteUserId")
    @ResponseBody
    public int toDeleteUserId(HttpServletRequest request, HttpServletResponse response) {
        String userId = request.getParameter("userId");
        int flag = userService.deleteUserId(userId);
        return flag;
    }
    
    
    
}
